//
//  KZStarView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/10/25.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZStarView.h"

@implementation KZStarView
{
    UIImageView *_view1;
    UIImageView *_view2;
    UIImageView *_view3;
    UIImageView *_view4;
    
    NSInteger _stars;
}
- (instancetype)initWithStars:(NSInteger)stars {
    if (self = [super init]) {
        _stars = stars;
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _view2 = [UIImageView new];
    _view2.tag = 11;
    [self addSubview:_view2];
    [_view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(self.mas_width).multipliedBy(0.18);
        make.right.equalTo(self.mas_centerX).offset(W(-0));
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    _view3 = [UIImageView new];
    _view3.tag = 12;
    [self addSubview:_view3];
    [_view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.equalTo(_view2);
        make.left.equalTo(self.mas_centerX).offset(W(0));
    }];
    
    _view1 = [UIImageView new];
    _view1.tag = 10;
    [self addSubview:_view1];
    [_view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(self.mas_width).multipliedBy(0.20);
        make.right.equalTo(_view2.mas_left).offset(W(-0));
        make.centerY.equalTo(_view2).offset(H(20));
    }];
    
    _view4 = [UIImageView new];
    _view4.tag = 13;
    [self addSubview:_view4];
    [_view4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.equalTo(_view1);
        make.left.equalTo(_view3.mas_right).offset(W(0));
    }];
    
    for (int i = 0; i < 4; i ++) {
        UIImageView *view = [self viewWithTag:i + 10];
        CATransform3D t = CATransform3DIdentity;
        t.m34 = -1.0 / 200;
        t = CATransform3DTranslate(t, 0, 0, 100);
        view.layer.transform = t;
        view.alpha = 0;

        if (i < _stars) {
            view.image = [UIImage imageNamed:@"kz-金星"];
        } else {
            view.image = [UIImage imageNamed:@"kz-蓝星"];
        }
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self startAnimation];
    });
    
}

- (void)startAnimation {
    

    [UIView animateKeyframesWithDuration:2 delay:0 options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.25 animations:^{
            _view1.layer.transform = CATransform3DIdentity;
            _view1.alpha = 1;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{
            _view2.layer.transform = CATransform3DIdentity;
            _view2.alpha = 1;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.25 animations:^{
            _view3.layer.transform = CATransform3DIdentity;
            _view3.alpha = 1;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
            _view4.layer.transform = CATransform3DIdentity;
            _view4.alpha = 1;
        }];
        
    } completion:nil];

}

@end
